package com.liang.crawlertx.task;

import com.liang.crawlertx.dao.NewDao;
import com.liang.crawlertx.pojo.News;
import com.liang.crawlertx.service.NewService;
import com.liang.crawlertx.utils.HttpClientUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.tomcat.jni.Local;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.time.ZoneOffset;
import java.util.Date;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

@Component
@Slf4j
public class NewTask {
    @Autowired
    private HttpClientUtil httpClientUtil;

    @Autowired
    private NewService newService;

    @Scheduled(fixedDelay=10000)
    public void NewTask(){
        log.info("=================爬取开始==================");
        String url="https://weixin.sogou.com/weixin?oq=&query=%E8%85%BE%E8%AE%AF&_sug_type_=1&sut=0&lkt=0%2C0%2C0&s_from=input&ri=0&_sug_=n&type=2&sst0=1620267479723&ie=utf8&p=40040108&dp=1&w=01015002&dr=1&page=1";

        String html = httpClientUtil.doGetHtml(url);

        this.parse(html);
        log.info("=================爬取结束==================");
    }

    private void parse(String html){
        Document doc = Jsoup.parse(html);
        Elements elements = doc.select("div.news-box > ul.news-list>li");
        for (Element element : elements) {

            String newId = element.select("li").attr("d");

            List<News> list = newService.findByNewId(newId);
            if(list.size()>0){
                continue;
            }


            String img = element.select("div.img-box>a>img").attr("src");
            String imgUrl = "http:"+img;
            String url = element.select("div.txt-box>h3>a").attr("href");
            String title = element.select("div.txt-box>h3>a").text();
            String content = element.select("div.txt-box>p.txt-info").text();
            String articleAccount = element.select("div.txt-box>div.s-p>a").text();
            String createdTime = element.select("div.txt-box>div.s-p>span.s2>script").html();
            Pattern p = Pattern.compile("[^0-9]");
            Matcher matcher = p.matcher(createdTime);
            String timeStamp = matcher.replaceAll("").trim();
            SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            String time = sdf.format(new Date(Long.parseLong(timeStamp)*1000L));

            log.info("newId:{}",newId);
            log.info("img:{}",imgUrl);
            log.info("url:{}",url);
            log.info("title:{}",title);
            log.info("content:{}",content);
            log.info("articleAccount:{}",articleAccount);
            log.info("createdTime:{}",time);
            log.info("-------------------");

            httpClientUtil.doGetImage(imgUrl);

            News news = new News();
            news.setNewId(newId);
            news.setTitle(title);
            news.setUrl(url);
            news.setContent(content);
            news.setArticleAccount(articleAccount);
            news.setCreatedTime(new Date(Long.parseLong(timeStamp)*1000L));
            news.setCrawledTime(LocalDateTime.now().plusHours(8));
            news.setImg(imgUrl);

            newService.saveNew(news);
        }
        
    }
}
